Some of programmers on this site say using "using namespace std;" is bad thing to use.
Why? What are the pros and cons?
Alright, I took your advice of indentation and did my best to fix the current code in terms of indentation. Please check it and spot the problem if the indentation is satisfactory, if not, then please tell me how to improve my indentation. Thanks a lot for the help so far guys.
Here is the indented code:
Code:
#include <iostream>
using namespace std;
int CycleLength(int x);
int main()
{
while(1)
{
int unsigned i,j,k,l,max_cycle=0;
//i,j are user inputs of the two numbers. k,l store initial values of i and j respectively to be printed out later.
cin>>i>>j;
k=i;
l=j;
if ((i==0)&&(j==0)) break;
if(i==j)
max_cycle=CycleLength(i);
else
if(i>j)
{
for(i;i>j;i--)
{
if (CycleLength(i)>max_cycle)
max_cycle=CycleLength(i);
}
}
for(i;i<j;i++)
{
if (CycleLength(i)>max_cycle)
max_cycle=CycleLength(i);
}
cout<<k<<" "<<l<<" "<<max_cycle<<endl;
}
}
int CycleLength(int x)
{
int count=1;
while(x>1)
{
if (x%2==!0)
x=3*x+1;
else
x=x/2;
count++;
}
return count;
}
I tried my best to satisfy everyone, but I did not understand why it was better to declare each variable in their own line and I also changed the variable 'max' to 'max_cycle'.